/*
 *  Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.afdor.rws.dtm.api.exception;

/**
 * Description: defined transaction exception code <br/>
 * ClassName: com.afdor.rws.dtm.api.exception.TransactionException.java <br/>
 *
 * @author <a href="mailto:scwsl@foxmail.com">悭梵</a>
 * @version V1.0
 * @date Created in 2019-01-20 11:23
 * @since JDK 1.8
 */
public enum TransactionExceptionCode {

    /**
     * 未知
     */
    UNKNOWN((byte) 0),

    /**
     * IO异常
     */
    IO((byte) -1),


    /**
     * 调用超时
     */
    TIMEOUT((byte) -2),

    /**
     * 全局(主)事务不存在
     */
    GLOBAL_TRANSACTION_NOT_EXIST((byte) 50),

    /**
     * 全局(主)事务非活动状态
     */
    GLOBAL_TRANSACTION_NOT_ACTIVE((byte) 50),

    /**
     * 全局(主)事务状态无效
     */
    GLOBAL_TRANSACTION_STATUS_INVALID((byte) 50),

    /**
     * 锁定资源冲突
     */
    LOCK_KEY_CONFLICT((byte) -10),


    /**
     * 分支事务回滚失败，可重试
     */
    BRANCH_ROLLBACK_FAILED_RETRIABLE((byte) 50),

    /**
     * 分支事务回滚失败，不可重试
     */
    BRANCH_ROLLBACK_FAILED_UNRETRIABLE((byte) 51),

    /**
     * 分支事务注册失败
     */
    BRANCH_REGISTER_FAILED((byte) 50),

    /**
     * 分支事务上报失败
     */
    BRANCH_REPORT_FAILED((byte) 51),

    /**
     * 上锁失败
     */
    LOCKABLE_CHECK_FAILED((byte) 52),

    /**
     * 分支事务不存在
     */
    BRANCH_TRANSACTION_NOT_EXIST((byte) 52),


    /**
     * 向分支发送提交指令失败
     */
    FAILED_TO_SEND_BRANCH_COMMIT_REQUEST((byte) 52),

    /**
     * 向分支发送回滚指令失败
     */
    FAILED_TO_SEND_BRANCH_ROLLBACK_REQUEST((byte) 52),

    /**
     * 添加分支事务失败
     */
    FAILED_TO_ADD_BRANCH((byte) 52),


    ;

    private final byte code;

    TransactionExceptionCode(byte code) {
        this.code = code;
    }


    /**
     * code to enum
     *
     * @param code
     * @return
     */
    public static TransactionExceptionCode valueOfCode(byte code) {
        for (TransactionExceptionCode value : TransactionExceptionCode.values()) {
            if (value.code == code) {
                return value;
            }
        }
        throw new IllegalArgumentException("Undefined TransactionExceptionCode[" + code + "]");
    }


}
